package com.qf.xiaomi.service.impl;

import com.qf.xiaomi.dao.impl.userDaoImpl;
import com.qf.xiaomi.dao.userDao;
import com.qf.xiaomi.pojo.TbUser;
import com.qf.xiaomi.service.userService;
import com.qf.xiaomi.utils.EmailUtils;
import com.qf.xiaomi.utils.MD5Utils;

import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class userServiceImpl implements userService {
      userDao dao=  new userDaoImpl();
    @Override
    public int addUser(TbUser user) {
        //设置数据
        //修改密码为MD5
        user.setPassword(MD5Utils.md5(user.getPassword()));
        //设置code，这个code是一个随机的内容，并不是真正的随机数
        user.setCode(UUID.randomUUID().toString().replace("-",""));
        //添加到数据库
        int result = dao.add(user);
        System.out.println(Thread.currentThread().getName());
        CompletableFuture.runAsync(()->{
//            try {
//                Thread.sleep(10000);
//                 System.out.println(Thread.currentThread().getName());
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            EmailUtils.sendEmail(user);
        });
        return result;
    }

    @Override
    public boolean selectUid(int id) {
        return dao.selectUid(id);
    }



    @Override
    public TbUser login(String username, String password) {
        TbUser user = dao.login(username);
        if (user!=null){
            String pwd = MD5Utils.md5(password);
            if(pwd.equals(user.getPassword())){
                return  user;
            }
        }
        return null;
    }

    @Override
    public int updateLoginTime(long id) {
        return dao.updateLoginTime(id);
    }

    @Override
    public TbUser IsAdmin(String username) {
        TbUser tbUser = dao.IsAdmin(username);

        return tbUser;
    }
}
